package com.jerry.constellation.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.jerry.constellation.R;
import com.jerry.constellation.Utils.AssetsUtils;
import com.jerry.constellation.bean.StarInfoBean;
import com.jerry.constellation.fragment.Lucky_Fragment;
import com.jerry.constellation.fragment.Me_Fragment;
import com.jerry.constellation.fragment.Partner_Fragment;
import com.jerry.constellation.fragment.Star_Fragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup main_rg;
    //声明Fragment
    private Fragment starFragment, partnerFragment, luckyFragment, meFragment;
    private FragmentManager fragmentManager;
    private FragmentManager supportFragmentManager;
    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化组件
        initView();
        //设置监听
        setOncheckedChange();
        //加载星座相关数据  /assets/xzcontent/xzcontent.json
        StarInfoBean infoBean=loadData();
        //将获取到的信息传送到各个Frag
        bundle = new Bundle();
        bundle.putSerializable("info",infoBean);
        //初始化fragment对象，并进行动态加载
        initFragment();
    }

    /*TODO：读取assets文件夹下的json文件
     * */
    private StarInfoBean loadData() {
        String json = AssetsUtils.getJsonfromAssets(MainActivity.this, "xzcontent/xzcontent.json");
        Gson gson = new Gson();
        StarInfoBean starInfoBean = gson.fromJson(json, StarInfoBean.class);
        AssetsUtils.saveBitmapFromAssets(MainActivity.this,starInfoBean);
        return starInfoBean;
    }


    private void initFragment() {
        starFragment = new Star_Fragment();
        starFragment.setArguments(bundle);
        partnerFragment = new Partner_Fragment();
        partnerFragment.setArguments(bundle);
        luckyFragment = new Lucky_Fragment();
        luckyFragment.setArguments(bundle);
        meFragment = new Me_Fragment();
        meFragment.setArguments(bundle);
        //动态加载
        addFragmentPage();
    }

    /*TODO:实现fragment的动态加载
     *
     *@des将主页当中的碎片一起加载进入布局，有用的显示，暂时无用的隐藏
     * */
    private void addFragmentPage() {
        //1.创建fragd额管理者对象
        supportFragmentManager = getSupportFragmentManager();
        //2.创建碎片处理事务对象
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        //3.将四个Frag添加到布局
        transaction.add(R.id.main_contain, starFragment);
        transaction.add(R.id.main_contain, partnerFragment);
        transaction.add(R.id.main_contain, luckyFragment);
        transaction.add(R.id.main_contain, meFragment);
        //4.隐藏后边的三个
        transaction.hide(partnerFragment);
        transaction.hide(luckyFragment);
        transaction.hide(meFragment);
        //5.提交碎片改变后的事务
        transaction.commit();
    }

    private void setOncheckedChange() {
        main_rg.setOnCheckedChangeListener(this);
    }

    private void initView() {
        main_rg = findViewById(R.id.main_rg);
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        switch (i) {
            case R.id.main_rb_star:
                transaction.hide(partnerFragment);
                transaction.hide(luckyFragment);
                transaction.hide(meFragment);
                transaction.show(starFragment);
                break;
            case R.id.main_rb_partner:
                transaction.hide(starFragment);
                transaction.hide(luckyFragment);
                transaction.hide(meFragment);
                transaction.show(partnerFragment);
                break;
            case R.id.main_rb_lucky:
                transaction.hide(starFragment);
                transaction.hide(partnerFragment);
                transaction.hide(meFragment);
                transaction.show(luckyFragment);
                break;
            case R.id.main_rb_me:
                transaction.hide(starFragment);
                transaction.hide(partnerFragment);
                transaction.hide(luckyFragment);
                transaction.show(meFragment);
                break;
            default:
                break;
        }
        transaction.commit();
    }
}